package com.fan.txl.mock.api.service.impl;

import com.fan.txl.mock.api.service.RpcService;
import com.fan.txl.mock.api.service.mock.MockAPIService;
import com.fan.txl.mock.api.service.mock.MockRpcService;

import java.util.concurrent.TimeUnit;
import retrofit2.Retrofit;
import retrofit2.mock.BehaviorDelegate;
import retrofit2.mock.MockRetrofit;
import retrofit2.mock.NetworkBehavior;

/**
 * Created by os on 2017/4/2.
 * Title
 */

public class MockAPIServiceImpl implements MockAPIService {

    private int mDelayTime=500;
    private Retrofit mRetrofit;

    public MockAPIServiceImpl(Retrofit retrofit) {
        this.mRetrofit = retrofit;
    }

    public   <T>BehaviorDelegate<T> createMockAPISrv(Retrofit retrofit, Class<T> service){
        NetworkBehavior behavior = NetworkBehavior.create();
        behavior.setDelay(mDelayTime, TimeUnit.MILLISECONDS);
        MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
                .networkBehavior(behavior)
                .build();
        BehaviorDelegate<T> delegate = mockRetrofit.create(service);
        return delegate;
    }

    @Override
    public RpcService getRpcService() {
        return new MockRpcService(createMockAPISrv(mRetrofit, RpcService.class));
    }

}
